home *** CD-ROM | disk | FTP | other *** search
- /*************************************************************************************
- #
- # PICT.c
- #
- # This segment handles PICT files.
- #
- # Author(s): Michael Marinkovich
- # Apple Developer Technical Support
- # marink@apple.com
- #
- # Modification History:
- #
- # 4/3/96 MWM Initial coding
- #
- # Copyright © 1992-96 Apple Computer, Inc., All Rights Reserved
- #
- #
- # You may incorporate this sample code into your applications without
- # restriction, though the sample code has been provided "AS IS" and the
- # responsibility for its operation is 100% yours. However, what you are
- # not permitted to do is to redistribute the source as "DSC Sample Code"
- # after having made changes. If you're going to re-distribute the source,
- # we require that you make it clear in the source that the code was
- # descended from Apple Sample Code, but that you've made changes.
- #
- *************************************************************************************/
-
- #include <Events.h>
- #include <ToolUtils.h>
- #include <Gestalt.h>
- #include <OSUtils.h>
- #include <Palettes.h>
-
- #include "App.h"
- #include "Proto.h"
-
-
- //----------------------------------------------------------------------
- //
- // OpenPictFile - with Spec, read file, convert to GWorld & return
- // the new world.
- //
- //----------------------------------------------------------------------
-
- OSErr OpenPictFile(FSSpec *spec, GWorldPtr *theWorld)
- {
- OSErr err;
- PicHandle thePict;
-
-
- err = ReadPICTFile(spec, &thePict);
-
- if (thePict != nil && err == noErr)
- {
- err = PictToWorld(thePict, theWorld);
- KillPicture(thePict);
- }
-
- return err;
-
- }
-
-
- //----------------------------------------------------------------------
- //
- // SavePictFile - with Spec, convert windows GWorld to a PICT and
- // save resulting PICT to the disk.
- //
- //----------------------------------------------------------------------
-
- OSErr SavePictFile(FSSpec *spec, GWorldPtr theWorld)
- {
- OSErr err;
- PicHandle thePict;
-
-
- err = WorldToPict(theWorld, &thePict);
-
- if (thePict != nil && err == noErr)
- {
- err = WritePICTFile(spec, thePict);
- KillPicture(thePict);
- }
-
- return err;
-
- }
-
- //----------------------------------------------------------------------
- //
- // ReadPICTFile - open and read disk data, returning PICT.
- //
- //
- //----------------------------------------------------------------------
-
- OSErr ReadPICTFile(FSSpec *spec, PicHandle *thePict)
- {
- OSErr err;
- long pictSize;
- long fileSize;
- short refNum;
-
- if (spec != nil)
- {
- SetCursor(*GetCursor(watchCursor)); // set the cursor to a watch while busy
-
- if (thePict != nil)
- *thePict = nil;
-
- err = FSpOpenDF(spec, fsRdWrShPerm, &refNum);
- if ( err == noErr )
- {
- err = GetEOF(refNum, &fileSize);
- if ( err == noErr )
- {
- err = SetFPos(refNum, fsFromMark, 512); // set position to our picture data
- if ( err == noErr )
- {
- pictSize = fileSize - 512;
- *thePict = (PicHandle)NewHandle(pictSize);
- err = MemError();
-
- if ( err == noErr && *thePict != nil )
- {
- HLock((Handle)*thePict);
- err = FSRead(refNum, &pictSize, **thePict); // read in the pict data
- HUnlock((Handle)*thePict);
- }
- }
- }
-
- FSClose( refNum ); // close the file
-
- SetCursor( &qd.arrow ); // set cursor back to arrow
- }
- }
- else
- err = paramErr;
-
-
- return err;
-
- }
-
-
-
- //----------------------------------------------------------------------
- //
- // WritePICTFile -
- //
- //
- //----------------------------------------------------------------------
-
- OSErr WritePICTFile(FSSpec *spec, PicHandle thePict)
- {
- OSErr err;
- long pictSize;
- long fileSize;
- long emptySize;
- short emptyBuffer;
- short refNum;
- short counter;
-
-
- if (spec != nil)
- {
- if (thePict != nil)
- {
- HLock((Handle)thePict);
- pictSize = GetHandleSize((Handle)thePict);
-
- emptySize = 1;
- emptyBuffer = 0;
-
- err = FSpCreate(spec, 'JVWR', kPICTType, smSystemScript);
-
- if (err == noErr)
- err = FSpOpenDF(spec,fsRdWrPerm, &refNum);
-
- if (err == noErr)
- err = SetFPos(refNum, fsFromStart , 0);
-
- if (err == noErr)
- for (counter = 0; counter < kHeaderSize; counter++)
- err = FSWrite(refNum, &emptySize, &emptyBuffer);
-
- if (err == noErr)
- err = FSWrite(refNum, &pictSize, *thePict);
-
- if (err == noErr)
- err = SetEOF(refNum, pictSize + kHeaderSize);
-
- if (err == noErr)
- err = FSClose(refNum);
-
- HUnlock((Handle)thePict);
- }
- else
- err = paramErr;
-
- }
- else
- err = paramErr;
-
- return err;
-
- }
-
-
- //----------------------------------------------------------------------
- //
- // PictToWorld - convert a PICT to a GWorld
- //
- //
- //----------------------------------------------------------------------
-
- OSErr PictToWorld(PicHandle thePict, GWorldPtr *theWorld)
- {
- OSErr err;
- CGrafPtr oldPort;
- GDHandle oldGD;
- PixMapHandle thePix;
- Rect theRect;
- short depth = 32;
-
-
- if (theWorld != nil && thePict != nil)
- {
- theRect = (**thePict).picFrame;
-
- *theWorld = nil;
-
- err = NewGWorld(theWorld, depth, &theRect, nil, nil, 0L);
-
- if (err == noErr && theWorld != nil)
- {
- thePix = GetGWorldPixMap(*theWorld);
- if (LockPixels(thePix))
- {
- GetGWorld(&oldPort, &oldGD);
-
- SetGWorld(*theWorld, nil);
- EraseRect(&theRect);
-
- DrawPicture(thePict, &theRect);
-
- SetGWorld(oldPort, oldGD);
-
- UnlockPixels(thePix);
- }
- }
- }
- else
- err = paramErr;
-
- return err;
-
- }
-
-
- //----------------------------------------------------------------------
- //
- // WorldToPict - convert a GWorld to a PICT
- //
- //
- //----------------------------------------------------------------------
-
- OSErr WorldToPict(GWorldPtr theWorld, PicHandle *thePict)
- {
- OSErr err = noErr;
- CGrafPtr oldPort;
- GDHandle oldGD;
- PixMapHandle thePix;
- PicHandle tempPict;
- Rect theRect;
-
-
- if (theWorld != nil && thePict != nil)
- {
- *thePict = nil;
- theRect = theWorld->portRect;
-
- thePix = GetGWorldPixMap(theWorld);
- if (LockPixels(thePix))
- {
- GetGWorld(&oldPort, &oldGD);
-
- SetGWorld(theWorld, nil);
-
- tempPict = OpenPicture(&theRect);
-
- CopyBits((BitMap *)*thePix, (BitMap *)*thePix,
- &theRect, &theRect, srcCopy, nil);
-
- ClosePicture();
-
- SetGWorld(oldPort, oldGD);
-
- err = QDError();
-
- if (tempPict != nil && err == noErr &&
- (**tempPict).picSize != 10)
- {
- *thePict = tempPict;
- }
- else
- tempPict = nil;
-
- UnlockPixels(thePix);
- }
-
- }
- else
- err = paramErr;
-
- return err;
-
- }
-
-
-
-
-
-
-
-